package com.igroupes.ovineherd.controller;

import com.google.common.base.Splitter;
import com.igroupes.ovineherd.aop.LoginUser;
import com.igroupes.ovineherd.constant.Constants;
import com.igroupes.ovineherd.dto.UserInfo;
import com.igroupes.ovineherd.dto.request.DeleteBatchRequest;
import com.igroupes.ovineherd.dto.request.GetRequestAttribute;
import com.igroupes.ovineherd.dto.request.ModelFieldBatchRequest;
import com.igroupes.ovineherd.dto.request.ModelFieldRequest;
import com.igroupes.ovineherd.service.AppService;
import com.igroupes.ovineherd.service.ModelApiService;
import com.igroupes.ovineherd.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("model")
@LoginUser
public class ModelApiController {
    @Autowired
    private ModelApiService modelApiService;
    @Autowired
    private AppService appService;

    /**
     * args格式： [{"field":"xxx","value":"xxx"}]
     *
     * @param modelId
     * @return
     */
    @PostMapping("/api/model/{modelId}")
    public ResultVO add(@NotBlank(message = "modelId is null") @PathVariable Long modelId, @RequestBody ModelFieldRequest request, UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.add(modelId, request, userInfo.getId());
    }

    @PutMapping("/api/model/{modelId}/{id}")
    public ResultVO update(@NotBlank(message = "modelId is null") @PathVariable Long modelId, @NotBlank(message = "id is null") @PathVariable Long id
            , @RequestBody ModelFieldRequest request, UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.update(modelId, id, request, userInfo.getId());
    }

    @PutMapping("/api/model/{modelId}/batch")
    public ResultVO updateBatch(@NotBlank(message = "modelId is null") @PathVariable Long modelId, @RequestBody ModelFieldBatchRequest request
            , UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.updateBatch(modelId, request, userInfo.getId());
    }


    @DeleteMapping("/api/model/{modelId}/{id}")
    public ResultVO delete(@NotBlank(message = "modelId is null") @PathVariable Long modelId, @NotNull(message = "id is null") @PathVariable Long id
            , UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.delete(modelId, id);
    }

    @DeleteMapping("/api/model/{modelId}/batch")
    public ResultVO deleteBatch(@NotBlank(message = "modelId is null") @PathVariable Long modelId, @RequestBody DeleteBatchRequest batchRequest
            , UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.deleteBatch(modelId, Splitter.on(Constants.COMMA).trimResults().splitToList(batchRequest.getIds()));
    }

    @PostMapping("/api/model/{modelId}/list")
    public ResultVO get(@NotBlank(message = "modelId is null") @PathVariable Long modelId, @RequestBody GetRequestAttribute attribute
            , UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.get(modelId, attribute);
    }

    @GetMapping("/api/model/{modelId}/{id}")
    public ResultVO getOne(@NotBlank(message = "modelId is null") @PathVariable Long modelId,
                           @NotBlank(message = "id is null") @PathVariable Long id
            , UserInfo userInfo) {
        appService.checkUserModel(userInfo.getId() , modelId);
        return modelApiService.getOne(modelId, id);
    }

}
